//
//  BRCouponsPopupView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/1/11.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRCouponsPopupView.h"

#define kContebtMargin_y 80

@interface BRCouponsPopupView ()
{
    CGFloat _content_h;
    UIView *_contentView;
    
    QMUILabel *_describeL;
    QMUILabel *_titleL;
    QMUILabel *_priceL1;
    QMUILabel *_priceL2;
    QMUIButton *_redeemBtn;
    QMUIButton *_closeBtn;
//
//    UIStackView *_allPointView;
//    QMUILabel *_allPointL;
}


@end

@implementation BRCouponsPopupView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

//        _content_h = kScreenHeight - (kSafeAreaTopHeight + kContebtMargin_y);
        _content_h = 550 + kSafeAreaBottomHeight;
        [self setUI];
    }
    return self;
}

- (void)hidd  {
    [self endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden =  YES;
    }];
}

- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeTranslation(0, -(self->_content_h));
    }];
}

- (void)doAction:(QMUIButton *)btn {
    
    if (self.confirmClickBlock) {
        self.confirmClickBlock(self.type);
    }
    
    if (self.type == 0) {
        [self hidd];
    }
    
    NSString *str = btn.currentTitle;
    if ([str isEqualToString:Localized(@"Rewards_buy")]) {
        
        NSLog(@"Rewards_buy");
        return;
    }
    
    if ([str isEqualToString:Localized(@"profile_confirm")]) {
        
        NSLog(@"profile_confirm");
        
        return;
    }
}

- (void)setPromoStr:(NSString *)promoStr {
    _promoStr = promoStr;
    _priceL1.text = promoStr.uppercaseString;
}

- (void)setType:(BRCouponType)type {
    _type = type;
    
//    self.couponView.couponType = type;
    _redeemBtn.enabled = YES;
    _priceL2.hidden = NO;
    _closeBtn.hidden = NO;
//    _allPointView.hidden = YES;
    if (type == BRCoupon_Promo) {
        // 优惠码兑换
        _priceL2.hidden = YES;
        _closeBtn.hidden = YES;
        _titleL.text = Localized(@"Rewards_promoCode");
        _describeL.text = Localized(@"Rewards_buyMsg");
        [_redeemBtn setTitle:Localized(@"public_done") forState:UIControlStateNormal];
        _priceL1.text = @"--";
        _priceL1.textColor = UIColor.qd_titleTextColor;
        _priceL1.font = [UIFont br_fontB:40];
//        [_describeL mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.couponView.mas_bottom).offset(80);
//        }];
        return;
    }
    if (type == BRCoupon_Buy) {
        // 购买
        _titleL.text = Localized(@"Rewards_buyCoupons");
        _describeL.text = Localized(@"Rewards_buyMsg");
        [_redeemBtn setTitle:Localized(@"Rewards_buy") forState:UIControlStateNormal];
        
        _priceL1.text = [[BRUserManager share] priceSymbol];
        _priceL1.textColor = UIColor.qd_titleTextColor;
        _priceL1.font = [UIFont br_fontE:24];
        
        _priceL2.text = @"0.00";
        _priceL2.textColor = UIColor.qd_titleTextColor;
        _priceL2.font = [UIFont br_fontB:40];
        
//        UIImage *bubble = UIImageMake(@"rewards_typeMoney");
//        //
//        self.couponView.bgImg.image = [bubble resizableImageWithCapInsets:UIEdgeInsetsMake(0 , 30, 0, 30) resizingMode:UIImageResizingModeStretch];
//        [_describeL mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.couponView.mas_bottom).offset(80);
//        }];
        return;
    }
    // BRCoupon_point
    [_redeemBtn setTitle:Localized(@"profile_confirm") forState:UIControlStateNormal];
    _titleL.text = Localized(@"Rewards_redeemCoupons");
    _describeL.text = Localized(@"Rewards_redeemMsg");
    _priceL1.text = @"0";
    _priceL1.textColor = UIColor.qd_titleTextColor;
    _priceL1.font = [UIFont br_fontB:40];
    _priceL2.text = Localized(@"Rewards_points");
    _priceL2.textColor = UIColor.qd_titleTextColor;
    _priceL2.font = [UIFont br_fontE:24];
//    _allPointView.hidden = NO;
//    [_describeL mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.couponView.mas_bottom).offset(102);
//    }];
//    NSString *imgName = [BRThemeManage br_walletCardBGName];
//    UIImage *bubble = UIImageMake(imgName);
//
//    self.couponView.bgImg.image = [bubble resizableImageWithCapInsets:UIEdgeInsetsMake(0 , 30, 0, 30) resizingMode:UIImageResizingModeStretch];
    
//    if (<#condition#>) {
    //积分不足是展示
//        _redeemBtn.enabled = NO;
//    }
}

- (void)setDto:(BRCouponModel *)dto {
    _dto = dto;
  
    
}

- (void)setPromoDto:(BRCouponModel *)promoDto {
    _promoDto = promoDto;
  
}

#pragma mark - Set UI --
- (void)setUI  {
    self.hidden = YES;
    self.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.38);
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, _content_h)];
    contentView.backgroundColor = UIColor.qd_backgroundColorLighten2;
    [self addSubview:contentView];
    [contentView  layoutIfNeeded];
    // 设置顶部圆角
    // 绘制圆角 需设置的圆角 使用"|"来组合
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:UIRectCornerTopLeft |
    UIRectCornerTopRight cornerRadii:CGSizeMake(32, 32)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

    // 设置大小
    maskLayer.frame = contentView.bounds;

    // 设置图形样子
    maskLayer.path = maskPath.CGPath;
    contentView.layer.mask = maskLayer;
    _contentView = contentView;
    
    UIView *couponViewBG = [[UIView alloc] init];
    couponViewBG.backgroundColor = UIColor.qd_backgroundColor2;
    [contentView addSubview:couponViewBG];
    [couponViewBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(60);
        make.centerX.equalTo(contentView);
        make.top.equalTo(contentView).offset(0);
    }];
    
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 52, 8, 44, 44)];
    [backBtn setBackgroundImage:UIImageMake(@"arrow_close_gray44") forState:UIControlStateNormal];
    [contentView addSubview:backBtn];
    _closeBtn = backBtn;
    [backBtn addTarget:self action:@selector(hidd) forControlEvents:UIControlEventTouchUpInside];
    
    QMUILabel *titleL = [[QMUILabel alloc] init];
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.textColor = UIColor.qd_titleTextColor;
    titleL.text = @"--";
    titleL.font = [UIFont br_fontB:17];
    [contentView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backBtn).offset(0);
        make.centerX.equalTo(contentView).offset(0);
    }];
    _titleL = titleL;
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end


@interface BRRedeemPopupView ()
{
    CGFloat _content_h;
    UIView *_contentView;
    
    QMUILabel *_describeL;
    QMUILabel *_titleL;
    QMUILabel *_priceL1;
    QMUILabel *_priceL2;
    QMUIButton *_redeemBtn;

}


@end

@implementation BRRedeemPopupView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _content_h = kSafeAreaBottomHeight + 334;
        [self setUI];
    }
    return self;
}

- (void)hidd  {
    [self endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden =  YES;
    }];
}

- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeTranslation(0, -(self->_content_h));
    }];
}

- (void)toRedeem {
    if (self.redeemBlock) self.redeemBlock(self.dto);
}

- (void)setDto:(BRCouponModel *)dto {
    _dto = dto;
    
    self.pointL.text = [NSString stringWithFormat:@"%ld %@",dto.point,Localized(@"points_pts")];
    
    NSTimeInterval secondsInDays = dto.validDay * 86400;
    // 获取当前时间的时间戳
    NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];

    // 计算7天后的时间戳
    NSTimeInterval newTimestamp = currentTimestamp + secondsInDays;
    self.textL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"order_expiryDate"),[BRTools getTimeFromTimestamp:(NSInteger)newTimestamp formatter:kTimeFormatter]];
}

- (void)setListDto:(BRCouponListModel *)listDto {
    
    _listDto = listDto;
   
    self.pointL.text = [NSString stringWithFormat:@"%ld %@",listDto.exchangePoint.integerValue,Localized(@"points_pts")];
    
    NSTimeInterval secondsInDays = listDto.exchangeEffectiveDays * 86400;
    // 获取当前时间的时间戳
    NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];

    // 计算7天后的时间戳
    NSTimeInterval newTimestamp = currentTimestamp + secondsInDays;
    self.textL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"order_expiryDate"),[BRTools getTimeFromTimestamp:(NSInteger)newTimestamp formatter:kTimeFormatter]];
}

#pragma mark - UI --
- (void)setUI {
    self.hidden = YES;
    self.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.38);
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, _content_h)];
    contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contentView];
    [contentView  layoutIfNeeded];
    // 设置顶部圆角
    // 绘制圆角 需设置的圆角 使用"|"来组合
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:UIRectCornerTopLeft |
    UIRectCornerTopRight cornerRadii:CGSizeMake(30, 30)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

    // 设置大小
    maskLayer.frame = contentView.bounds;

    // 设置图形样子
    maskLayer.path = maskPath.CGPath;
    contentView.layer.mask = maskLayer;
    _contentView = contentView;
    
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 58, 0, 44, 44)];
    [backBtn setBackgroundImage:UIImageMake(@"arrow_close_gray44") forState:UIControlStateNormal];
    [contentView addSubview:backBtn];
//    _closeBtn = backBtn;
    [backBtn addTarget:self action:@selector(hidd) forControlEvents:UIControlEventTouchUpInside];
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"points_redeemCoupon") textColor:kColorTitle font:[UIFont fontS:17] textAlignment:NSTextAlignmentCenter];
    [contentView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(backBtn.mas_centerY).offset(0);
    }];
    // ------------优惠券样式----------------
    UIView *couponView = [[UIView alloc] init];
    couponView.layer.cornerRadius = 10;
    couponView.layer.masksToBounds = YES;
    couponView.backgroundColor = kColorBGF5;
    [contentView addSubview:couponView];
    [couponView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.right.offset(-28);
        make.top.offset(64);
        make.height.mas_equalTo(77);
    }];
    self.titleL = [BRToolsView br_label:@"" textColor:kColorTitle font:[UIFont fontS:17]];
    [couponView addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(14);
    }];
    self.textL = [BRToolsView br_label:@"" textColor:kColorTitle66B font:[UIFont fontM:13]];
    [couponView addSubview:self.textL];
    [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.bottom.offset(-14);
    }];
    
    //-----------
    QMUILabel *pointL = [BRToolsView br_label:@"-- Points" textColor:kColorTitle font:[UIFont fontS:20] textAlignment:NSTextAlignmentCenter];
    [contentView addSubview:pointL];
    [pointL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(couponView.mas_bottom).offset(20);
    }];
    _pointL = pointL;
    
    QMUILabel *pointSubL = [BRToolsView br_label:Localized(@"coupon_afterRedeenText") textColor:kColorTitle66B font:[UIFont fontM:14] textAlignment:NSTextAlignmentCenter];
    pointSubL.numberOfLines = 0;
    [contentView addSubview:pointSubL];
    [pointSubL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(pointL.mas_bottom).offset(6);
        make.right.offset(-(28));
    }];
    
    
    _redeemBtn = [BRToolsView br_btnBGImg:Localized(@"coupon_redeem")];
    [contentView addSubview:_redeemBtn];
    [_redeemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.height.mas_equalTo(52);
        make.bottom.offset(-(10 + kSafeAreaBottomHeight));
    }];
    [_redeemBtn addTarget:self action:@selector(toRedeem) forControlEvents:UIControlEventTouchUpInside];
}

@end
